//app.js
App({
    onLaunch: function () { 
        var app = this;
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        app.globalData.extJson = wx.getExtConfigSync();  
        app.http = require('/pages/http/api.js');
        app.common = require('/utils/js/common.js');
        // app.globalData.UploadUrl ="http://192.168.1.84:8008/MiniCommon/UploadImg";
        //app.globalData.UploadUrl = "https://wxapp.aimeifa.com/MiniCommon/UploadImg";
 
    },
    globalData: {
        userInfo: null
    },
    getUserall:function(func){
      var app = this,
        appID = app.globalData.extJson.Appid;
      if (app.globalData.userInfo) {
        func(app.globalData.userInfo);
        return;
      }
      wx.getSetting({
        success(res) {
          var userAuth = res.authSetting['scope.userInfo'];
          if (userAuth===true) {
            app._login(appID,func);
          } else if (userAuth===false){
            wx.showModal({
              title: '提示',
              content: '请授权后再继续操作',
              showCancel:false,
              success:function(){
                app._userSetting(func);
              }
            })
          }else{
            wx.authorize({
              scope: 'scope.userInfo',
              success: (res) => {
                app._login(appID,func);
              },
              fail:(res)=>{
                wx.showModal({
                  title: '提示',
                  content: '请授权后再继续操作',
                  showCancel: false,
                  success: function () {
                    app._userSetting(func);
                  }
                })
              }
            });
          }
        }
      });
    },
    _login: function (appID,func){
      var app=this;
      wx.login({
        success: res => {
          wx.showLoading({
            title: '登录中..',
          })
          var code = res.code;
          wx.getUserInfo({
            lang: "zh_CN",
            success: res => {
              app.log('getUserInfo', res);
              app.http.POST("/User/MiniUserLogin", {
                appid: appID,
                code: code,
                encryptedData: res.encryptedData,
                iv: res.iv,
                rl: 2//老板端
              },
                (err, data) => {
                  if (data.code == 1) {
                    app.globalData.userInfo = data.data.userData;
                    app.http.setCookie(data.data.cookie);
                    func(app.globalData.userInfo);
                  }
                  wx.hideLoading();
                })
            }
          })
        }
      })
    },
    _userSetting: function (func){
      var app=this;
      var appID = app.globalData.extJson.Appid;
      wx.openSetting({
        success: (res) => {
          res.authSetting = {
            "scope.userInfo": true
          },
            app.getUserall(func);  
        },
        fail: function (res) {
          wx.showToast({
            title: '获取用户信息失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }, 
    log: function () {
        console.log.apply(console, arguments);
    },
 })